<?php
require_once 'function.php';

class login extends spController
{
	function userlogin(){
		$username = $this -> spArgs("username");
		$password = $this -> spArgs("password");
		
		/*$username = 's091633';
		$password = 'WuDiLtH';
		$username2 = 's091669';
		$password2 = 's091669';*/

		//$xml1 = getXML('http://221.7.207.123:8065/opac/webemail/rss?rdid='.$username.'&pwd='.$password.'&cmd=listRdloan',$username);
		$xml = getXML('http://10.1.58.13/opac/webemail/rss?rdid='.$username.'&pwd='.$password.'&cmd=listRdloan',$username);
		//$xml2 = getXML('http://221.7.207.123:8065/opac/webemail/rss?rdid='.$username2.'&pwd='.$password2.'&cmd=listRdloan',$username2);
		//$xml2 = getXML('http://10.1.58.13/opac/webemail/rss?rdid='.$username2.'&pwd='.$password2.'&cmd=listRdloan',$username2);
		
		$dom = new DOMDocument();
		$dom -> preserveWhiteSpace = FALSE;
		$dom -> loadXML($xml);
		$titles = $dom -> getElementsByTagName("title");
		$message = $titles -> item(0) -> nodeValue;
		if($message != "读者当前借阅图书列表" || $message == "错误信息"){
			echo $xml;
		}
		else
		{
			$hash = md5($username.$password);
			$conditions = array('username' => $username);
			$row = array('hash' => $hash,'lastlogintime' => date('Y-m-d H:i:s'));
			$users = spClass("users");
			$users -> update($conditions, $row);
			file_put_contents(APP_PATH.'/xml/'.$username.'_listRdloan.xml', $xml);
			echo $xml;
			
		}
		
		/*if(file_get_contents(APP_PATH.'/xml/'.$username.'_listRdloan.xml') == $xml){
			echo $xml;
		}
		else
		{
			echo "Can't write ".APP_PATH."/xml/".$username."_listRdloan.xml";
		}*/
		

		
	}
	
	function userinit(){
		$hash = $this -> spArgs("hash");
		$conditions = array('hash' => $hash);
		$users = spClass("users");
		$username = $users -> find($conditions,"hash","username");
		$username = $username["username"];
		
		
		/*$username = 's091633';
		$password = 'WuDiLtH';
		$username2 = 's091669';
		$password2 = 's091669';*/

		//$xml1 = getXML('http://221.7.207.123:8065/opac/webemail/rss?rdid='.$username.'&pwd='.$password.'&cmd=listRdloan',$username);
		//$xml = getXML('http://10.1.58.13/opac/webemail/rss?rdid='.$username.'&pwd='.$password.'&cmd=listRdloan',$username);
		//$xml = getXML('http://221.7.207.123:8065/opac/webemail/rss?rdid='.$username.'&pwd='.$password.'&cmd=listRdloan',$username);
		//$xml2 = getXML('http://221.7.207.123:8065/opac/webemail/rss?rdid='.$username2.'&pwd='.$password2.'&cmd=listRdloan',$username2);
		//$xml2 = getXML('http://10.1.58.13/opac/webemail/rss?rdid='.$username2.'&pwd='.$password2.'&cmd=listRdloan',$username2);
		
		$xml = file_get_contents(APP_PATH.'/xml/'.$username.'_listRdloan.xml');
		
		$dom = new DOMDocument();
		$dom -> preserveWhiteSpace = FALSE;
		$dom -> loadXML($xml);
		$items = $dom -> getElementsByTagName("item");
		foreach ($items as $item){
			$links = $item -> getElementsByTagName("link");
			$link .= $links -> item(0) -> nodeValue.' ';
		}
		
		$linkarr = split(" ", $link);
		$linkarr = array_filter($linkarr);
		
		/*foreach ($linkarr as $link)
		{
			echo '<textarea rows="20" cols="20" name="xml3">';
			echo postXML($link,$username);
			echo '</textarea>';
		}
		*/
		$xml1 = postXML($linkarr[0], $username);
		file_put_contents(APP_PATH.'/xml/'.$username.'_DoReloan.xml', $xml1);
		
		if(file_get_contents(APP_PATH.'/xml/'.$username.'_DoReloan.xml') == $xml1){
			//echo $xml1;
		}
		else
		{
			echo "Can't write ".APP_PATH."/xml/".$username."_DoReloan.xml";
		}
		
		$xml2 = $xml1;
	
		$checklogin = new SimpleXMLElement($xml2);
	
		if (($checklogin -> loginFlag) != "YES" || $checklogin -> loginFlag == "NO"){
			echo "登录信息错误，请重新登录。";
		}
	

		$dom = new DOMDocument();
		$dom -> preserveWhiteSpace = FALSE;
		$dom -> loadXML($xml2);
		$callnos = $dom -> getElementsByTagName("CALLNO");
		
		$callnodata = "";
		
		foreach ($callnos as $callno){
			$callnodata .= $callno -> nodeValue.' ';
		}
		
		$callnoarr = split(" ", $callnodata);
		$callnoarr = array_filter($callnoarr);
		/*echo '<textarea rows="20" cols="20" name="xml4">';
		
		print_r($arr);
		echo '</textarea>';*/
		
		$regexpdata = "";
		
		foreach ($callnoarr as $regexp) {
			$regexpdata .= ztf_regexp($regexp).' ';
		}
		
		$regexparr = split(" ", $regexpdata);
		$regexparr = array_unique(array_filter($regexparr));
		
		//print_r($regexparr);
		
		$loushiapp = spClass('loushi');
		
		foreach ($regexparr as $ztf){
			$conditions = array('category' => $ztf);
			$loushis = $loushiapp -> spCache(3600) -> findAll($conditions,"id");
			//$loushidata .= $loushiapp -> find($conditions,$fields);
		}
		
		$loushiid = "";
		$loushiname = "";
		
		/*foreach ($loushis as $loushi){
			$loushiname .= $loushi["name"]." ";
		}*/
		
		$loushiidarr = split(" ", $loushiid);
		$loushiidarr = array_filter($loushiidarr);
		
		$loushinamearr = split(" ", $loushiname);
		$loushinamearr = array_filter($loushinamearr);
		
		/*echo '<textarea rows="20" cols="20" name="loushiid">';
		print_r($loushiidarr);
		echo '</textarea>';
		
		echo '<textarea rows="20" cols="20" name="loushiid">';
		print_r($loushinamearr);
		echo '</textarea>';*/
		
		$this -> loushiresult = $loushis;
		$this -> display("userinfo.xml");
	}
	
}